﻿using DongFangGuoKai.WMSPlatform.Commn.ExtendModule;
using DongFangGuoKai.WMSPlatform.Core;
using DongFangGuoKai.WMSPlatform.Domain.Models;
using DongFangGuoKai.WMSPlatform.Service.LogApp;
using DongFangGuoKai.WMSPlatform.Service.OutboundOrderDetail;
using DongFangGuoKai.WMSPlatform.Service.OutboundOrderDetail.DTO;
using DongFangGuoKai.WMSPlatform.WebApi.Utility.Filters;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Serilog;

namespace DongFangGuoKai.WMSPlatform.WebApi.Controllers.OrderOutbound
{
    /// <summary>
    /// 出库
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TShippingOrderDetailController : ApiBaseController
    {
        private readonly ITOutboundOrderDetailService tOutboundOrderDetailService;
        private readonly Repository<t_ShippingOrderDetail> shippingOrderrepost;
        private readonly Repository<t_ShippingLockCell> shippinglockcellrepost;
        private readonly ILogger<TShippingOrderDetailController> shippingorderlogger;
        private readonly Repository<t_Instruct> instructrepost;
        private readonly ILogService interfaceLogService;

        public TShippingOrderDetailController(ITOutboundOrderDetailService tOutboundOrderDetailService,
            Repository<t_ShippingOrderDetail> shippingOrderrepost,
            ILogger<TShippingOrderDetailController> shippingorderlogger,
            ILogService interfaceLogService
            ):base(interfaceLogService)
        {
            this.tOutboundOrderDetailService = tOutboundOrderDetailService;
            this.shippingOrderrepost = shippingOrderrepost;

            instructrepost = shippingOrderrepost.ChangeRepository<Repository<t_Instruct>>();
            shippinglockcellrepost = shippingOrderrepost.ChangeRepository<Repository<t_ShippingLockCell>>();
            this.shippingorderlogger = shippingorderlogger;
            this.interfaceLogService = interfaceLogService;
        }

        /// <summary>
        /// 查询出库单
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetShippingOrderList(int pageIndex, int pageSize)
        {
            var list = shippingOrderrepost.GetList().Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            List<GetChukuDanDTO> resList = new List<GetChukuDanDTO>();
            if (list != null)
            {
                foreach (var item in list)
                {
                    var lockcells = shippinglockcellrepost.GetList(s=>s.OrderId == item.Id);
                    resList.Add(new GetChukuDanDTO()
                    {
                        OrderSn = item.OrderSN,
                        OutBoundNo = item.OutBoundNo,
                        CreateTime = item.CreateDate,
                        
                        CreateUserName = item.CreateUserName,
                        AuditUserName = item.AuditUserName,
                        //shippingOrders = lockcells,
                    });
                }
                return Success(resList);
            }
            return Failed("查询失败！");
        }

        /// <summary>
        /// 执行出库
        /// </summary>
        /// <param name="OutBoundNo"></param>
        /// <returns></returns>
        //[HttpPost]
        //public IActionResult ExecuteOutBound(string OutBoundNo)
        //{
        //    string errorMsg = string.Empty;
        //    TInterfaceLog log;
        //    var IsOk = tOutboundOrderDetailService.ExecuteOutBound(OutBoundNo, out errorMsg);

        //    CreateInterfaceLog("执行出库任务", "/api/TShippingOrderDetail/ExecuteOutBound", 2, OutBoundNo, out log);
        //    if (IsOk)
        //    {
        //        shippingorderlogger.LogInformation("方法名：ExecuteOutBound, 信息：" + errorMsg + "参数：" + OutBoundNo);
        //        return Success(errorMsg);
        //    }
        //    shippingorderlogger.LogError("方法名：ExecuteOutBound , 错误信息：" + errorMsg + "参数：" + OutBoundNo);
        //    return Failed(errorMsg);
        //}

        /// <summary>
        /// 样品到输送线
        /// </summary>
        /// <param name="reqCode"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult SampleToConveyor(string reqCode)
        {
            Guid guid;
            t_InterfaceLog log;
            if (!Guid.TryParse(reqCode, out guid))
            {
                return Failed("Guid不对");
            }
            var instruct = instructrepost.GetFirst(i => i.BusinessGroupId == guid);
            CreateInterfaceLog("完成出库任务", "/api/TShippingOrderDetail/CompleteOutBound", 1, reqCode, out log);
            if (instruct == null)
            {
                shippingorderlogger.LogError("方法名：CompleteOutBound 错误信息：未查询到对应指令" + "参数：" + reqCode);
                return Failed("未查询到对应指令");
            }
            string errorMsg = string.Empty;
            var IsOk = tOutboundOrderDetailService.SampleToConveyor(instruct,out errorMsg);

            if (IsOk)
            {
                shippingorderlogger.LogInformation("方法名：CompleteOutBound 信息：" + errorMsg + "参数：" + reqCode);

                return Success(errorMsg);
            }
            shippingorderlogger.LogError("方法名：CompleteOutBound 错误信息：" + errorMsg + "参数：" + reqCode);
            return Failed(errorMsg);
        }
        /// <summary>
        /// 添加出库单
        /// </summary>
        /// <param name="addOutBoundOrder"></param>
        /// <returns></returns>
        [HttpPost]
        [TypeFilter(typeof(CtmLogActionAsyncFilterAttribute))]
        public IActionResult AddOutboundOrders(AddOutBoundOrder addOutBoundOrder)
        {
            string errorMsg = string.Empty;
            t_InterfaceLog log;
            var IsOk = tOutboundOrderDetailService.AddOutboundOrders(addOutBoundOrder, out errorMsg);

            CreateInterfaceLog("Lims下传出库单", "/api/TShippingOrderDetail/AddOutboundOrders", 2, addOutBoundOrder.ToJson(), out log);
            if (IsOk)
            {
                shippingorderlogger.LogError("方法名：AddOutboundOrders 信息：" + errorMsg + "参数：" + addOutBoundOrder.ToJson());
                return Success(errorMsg);
            }
            shippingorderlogger.LogError("方法名：AddOutboundOrders 错误信息：" + errorMsg + "参数：" + addOutBoundOrder.ToJson());
            return Failed(errorMsg);
        }
    }
}
